package com.syqc.monitor.ui.carinfo

import android.view.View
import android.widget.ImageView
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.syqc.comlib.base.LazyLoadSirFragment
import com.syqc.comlib.callback.EmptyCallback
import com.syqc.comlib.databinding.IncludeRecyclerMathMBinding
import com.syqc.comlib.utils.loadStringImgNoneError
import com.syqc.monitor.R
import com.syqc.monitor.ui.newaddcar.PhotoViewPopup

/** @Author Jasper @Time 2021/2/27 @Desc todo */
class CarPhotosFragment :
    LazyLoadSirFragment<IncludeRecyclerMathMBinding>(IncludeRecyclerMathMBinding::inflate) {
    private val mViewModel by activityViewModels<CarInfoViewModel>()
    private val mAdapter by lazy { PhotosAdapter() }
    private val mPhotoView by lazy { PhotoViewPopup(requireActivity()) }
    var type: String=""

    companion object {
        fun getInstance(type: String): CarPhotosFragment {
            val mFragment = CarPhotosFragment()
            mFragment.type = type
            return mFragment
        }
    }


    override fun initView(view: View) {
        binding.recyclerView.layoutManager = LinearLayoutManager(requireContext())
        binding.recyclerView.adapter = mAdapter
        loadService.showCallback(EmptyCallback::class.java)
    }

    override fun initData() {
        mViewModel.gitPic(type)
        when (type) {
            "car" -> {
                mViewModel.carPhotoLive.observe(this) {
                    mAdapter.setList(it)
                    if (it.isNotEmpty()) loadService.showSuccess()
                }
            }
            "tmn" -> {
                mViewModel.tmnPhotoLive.observe(this) {
                    mAdapter.setList(it)
                    if (it.isNotEmpty()) loadService.showSuccess()
                }
            }
            "drv" -> {
                mViewModel.drvPhotoLive.observe(this) {
                    mAdapter.setList(it)
                    if (it.isNotEmpty()) loadService.showSuccess()
                }
            }
        }
    }


    override fun initEvent() {
        mAdapter.setOnItemClickListener { adapter, view, position ->
            mPhotoView.show(mAdapter.data[position])
        }
    }

    class PhotosAdapter : BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_car_photo) {
        override fun convert(holder: BaseViewHolder, item: String) {
            item.run {
                holder.getView<ImageView>(R.id.iv_car_photo).loadStringImgNoneError(context, this)
            }
        }
    }
}